## build a hand-checkable example
##
## simple 10x10 matrix, 2 series
##
ts1.data <- as.vector(c(2.00, 3.00, 4.00, 4.00, 5.00, 5.00, 4.00, 3.00, 3.00, 2.00, 1.00, 1.00))
ts2.data <- as.vector(c(1.00, 2.00, 2.00, 3.00, 3.00, 3.00, 5.00, 5.00, 4.00, 3.00, 2.00, 1.00))

## normalize the data
ts1.data <- (ts1.data-mean(ts1.data))/var(ts1.data)
ts2.data <- (ts2.data-mean(ts2.data))/var(ts2.data)


## get the max series length
ts.length <- 12

## plot the original data
plot(NULL, xlim=c(0,ts.length), ylim=c(-1.5,1.5), main="Development time for trajectory1 and trajectory2", xlab="Time ticks", ylab="Normalized DevTime")
lines(ts1.data, lwd=2, col="mediumspringgreen")
points(ts1.data, pch=3, cex=1, col="mediumspringgreen")
lines(ts2.data, lwd=2, col="royalblue")
points(ts2.data, pch=18, cex=1.5, lwd=2, col="royalblue")

## do DTW
par(mfrow=c(1,1))
library(dtw)
alignment<-dtw(x=ts1.data,y=ts2.data, step.pattern=asymmetricP0, keep=T);
plot(alignment$index1,alignment$index2,main="Warping function");
dtwPlotThreeWay(alignment, main="Both time series and warping curve")
dtwPlotTwoWay(alignment, main="Both time series and warping curve")
dtwPlotDensity(alignment, main="Trajectory1/Trajectory2: symmetric alignment, no constraints")


par(mfrow=c(1,2));
wq<-warp(alignment,index.template=FALSE);
wt<-warp(alignment,index.template=TRUE);

plot(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), ylim=c(0,1) ,pch=18, cex=1.5, lwd=2, col="royalblue", main="Warping query")
lines(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=2, col="royalblue");
lines(cbind(seq(0,dim(ts1.data)[1]-1), as.vector(ts1.data[,1])[wq]), lwd=2, col="mediumspringgreen");

plot(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), ylim=c(0,1), pch=18, cex=1.5, lwd=2, col="mediumspringgreen", main="Warping template")
lines(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), lwd=2, col="mediumspringgreen");
lines(cbind(seq(0,dim(ts2.data)[1]-1), as.vector(ts2.data[,1])[wt]), lwd=2, col="royalblue");

######################################
######################################
# [2.0] CONTROL SECTION, INPUT DATA
#
#
par(mfrow=c(1,2));

## plot the original data
plot(NULL, xlim=c(0,ts.length), ylim=c(0,1), main="R input", xlab="Time ticks", ylab="Normalized DevTime")
lines(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), lwd=2, col="mediumspringgreen")
points(cbind(seq(0,dim(ts1.data)[1]-1), ts1.data), pch=3, cex=1, col="mediumspringgreen")
lines(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=2, col="royalblue")
points(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), pch=18, cex=1.5, lwd=2, col="royalblue")

lines(cbind(c(0, 2),c(0.95, 0.95)), lwd=2, col="mediumspringgreen")
text(2.2, 0.95, labels="trajectory1", cex=0.9, pos=4)
lines(cbind(c(0, 2),c(0.9, 0.9)), lwd=2, col="royalblue")
text(2.2, 0.9, labels="trajectory2", cex=0.9, pos=4)

## plot the Java extracted data
b_1<-c(0.00,0.00,0.50,0.26,0.31,0.24,0.30,0.00,0.00,0.30,0.26,0.24,0.33,0.28,0.00)
b_2<-c(0.00,0.22,0.22,0.28,0.00,0.00,0.83,0.93,1.00,0.81,0.81,0.00,0.00,0.26,0.22)
plot(NULL, xlim=c(0,ts.length), ylim=c(0,1), main="Java input", xlab="Time ticks", ylab="Normalized DevTime")
lines(cbind(seq(0,dim(ts2.data)[1]-1), b_2), lwd=2, col="mediumspringgreen")
points(cbind(seq(0,dim(ts2.data)[1]-1), b_2), pch=3, cex=1, col="mediumspringgreen")
points(cbind(seq(0,dim(ts1.data)[1]-1), b_1),pch=18, cex=1.5, lwd=2, col="royalblue")
lines(cbind(seq(0,dim(ts1.data)[1]-1), b_1), lwd=2, col="royalblue");
lines(cbind(c(0, 2),c(0.95, 0.95)), lwd=2, col="mediumspringgreen")
text(2.2, 0.95, labels="trajectory1", cex=0.9, pos=4)
lines(cbind(c(0, 2),c(0.9, 0.9)), lwd=2, col="royalblue")
text(2.2, 0.9, labels="trajectory2", cex=0.9, pos=4)

######################################
######################################
# [3.0] CONTROL SECTION, OUTPUT DATA
#
#
par(mfrow=c(1,2));
wq<-warp(alignment,index.template=FALSE);

## plot the original data
plot(NULL, xlim=c(0, ts.length), ylim=c(0,1), main="Warping query, R", xlab="Time ticks", ylab="Normalized DevTime")
lines(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), lwd=2, col="royalblue")
points(cbind(seq(0,dim(ts2.data)[1]-1), ts2.data), pch=18, cex=1.5, lwd=2, col="royalblue")
points(cbind(seq(0,dim(ts1.data)[1]-1), as.vector(ts1.data[,1])[wq]), pch=3, cex=1, col="mediumspringgreen")
lines(cbind(seq(0,dim(ts1.data)[1]-1), as.vector(ts1.data[,1])[wq]), lwd=2, col="mediumspringgreen");

lines(cbind(c(5, 7),c(0.98, 0.98)), lwd=2, col="mediumspringgreen")
text(7.2, 0.98, labels="trajectory1, QUERY", cex=0.9, pos=4)
lines(cbind(c(5, 7),c(0.93, 0.93)), lwd=2, col="royalblue")
text(7.2, 0.93, labels="trajectory2, TEMPLATE", cex=0.9, pos=4)



## plot the Java extracted data
a_1<-c(0.00,0.00,0.00,0.00,0.00,0.00,0.50,0.26,0.31,0.24,0.30,0.00,0.00,0.24,0.0)
a_2<-c(0.22,0.00,0.83,0.93,1.00,0.81,0.81,0.00,0.00,0.26,0.26,0.26,0.26,0.26,0.22)
plot(NULL, xlim=c(0, ts.length), ylim=c(0,1), main="Warping query, R", xlab="Time ticks", ylab="Normalized DevTime")
points(cbind(seq(0,dim(ts1.data)[1]-1), b_1),pch=18, cex=1.5, lwd=2, col="royalblue")
lines(cbind(seq(0,dim(ts1.data)[1]-1), b_1), lwd=2, col="royalblue");
